package com.xiaoxian.user.service.impl;

import com.xiaoxian.basic.commoneumn.ErrorCode;
import com.xiaoxian.basic.constant.Constant;
import com.xiaoxian.basic.domain.dto.SmsCode;
import com.xiaoxian.basic.exception.CustomException;
import com.xiaoxian.basic.util.AjaxResult;
import com.xiaoxian.basic.util.MD5Utils;
import com.xiaoxian.basic.util.StrUtils;
import com.xiaoxian.user.domain.LoginInfo;
import com.xiaoxian.user.domain.User;
import com.xiaoxian.user.mapper.LogininfoMapper;
import com.xiaoxian.user.mapper.UserMapper;
import com.xiaoxian.user.service.IUserService;
import com.xiaoxian.basic.service.impl.BaseServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author xiaoxian
 * @since 2022-12-21
 */
@Service
public class  UserServiceImpl extends BaseServiceImpl<User> implements IUserService {
    @Autowired
    private UserMapper userMapper;
    @Autowired
    private RedisTemplate redisTemplate;
    @Autowired
    private LogininfoMapper logininfoMapper;
    /**
     * 注册
     * @param smsCode
     * @return
     */
    @Override
    @Transactional
    public AjaxResult registerPhone(SmsCode smsCode) {
        //参数校验
        //判断电话是否存在
        //查询电话
        User user = userMapper.findByPhone(smsCode.getPhone());

        if(user!=null){
            throw new CustomException(ErrorCode.PHONE_EXIST.getCode());
        }
        //判断两次密码是否一致
        if(!smsCode.getPassword().equals(smsCode.getConfirmpassword())){
            throw new CustomException(ErrorCode.SHOP_PASSWORD_DEF.getCode());
        }
        //判断短信验证码是否过期
        //拼接key
        String smsKey=smsCode.getPhone()+ Constant.REGISTER_KEY;
        String smsValue =(String) redisTemplate.opsForValue().get(smsKey);
        if(smsValue!=null){
            //判断验证码是否和前面得一直
            String smsCodeStr = smsValue.split(":")[0];
            if(smsCode.getSmsCode().equalsIgnoreCase(smsCodeStr)){
                //设置盐值
                String salt = StrUtils.getComplexRandomString(64);
                //md5加密
                String pwdMD5 = MD5Utils.encrypByMd5(salt + smsCode.getPassword());
                LoginInfo loginInfo = setLoginInfo(smsCode, salt, pwdMD5);
                //插入数据库
                logininfoMapper.add(loginInfo);
                user=setUser(loginInfo);
                //插入数据库
                userMapper.add(user);
                return AjaxResult.my();
            }else {
                throw new CustomException(ErrorCode.SMS_CODE_DEF.getCode());
            }

        }
        return AjaxResult.my().setMsg("验证码过期").setSuccess(false);
    }

    private User setUser(LoginInfo loginInfo) {
        //创建User对象
        User user = new User();
        //将logininfo复制给user
        BeanUtils.copyProperties(loginInfo,user);
        user.setCreatetime(new Date());
        user.setLogininfoId(loginInfo.getId());
        return user;
    }


    private LoginInfo setLoginInfo(SmsCode smsCode, String salt, String pwdMD5) {
        //创建logininfo对象
        LoginInfo loginInfo = new LoginInfo();
        //设置密码
        loginInfo.setPassword(pwdMD5);
        //设置盐值
        loginInfo.setSalt(salt);
        //设置用户名
        loginInfo.setUsername(smsCode.getPhone());
        //设置电话
        loginInfo.setPhone(smsCode.getPhone());
        //设置身份
        loginInfo.setType(1);
        //设置状态
        loginInfo.setState(1);
        return loginInfo;
    }
}
